#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
//    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint | Qt::WindowTitleHint);
    this->setWindowTitle("点名器");
    ui->pushButton->setEnabled(false);
    ui->pushButton_2->setEnabled(false);
    ui->actionopenfile->setShortcut(QKeySequence("Ctrl+F"));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setPBtnEnable(bool flag)
{
    ui->actionopenfile->setEnabled(flag);
}

void MainWindow::updateLabel(QString name)
{
    ui->label->setText(name);
}

void MainWindow::on_pushButton_clicked()
{
    thread->start();
}

void MainWindow::on_pushButton_2_clicked()
{
    thread->stop();
}

void MainWindow::on_actionopenfile_triggered()
{
    QString filename = QFileDialog::getOpenFileName(this, "选择点名册", "C:/Users/NAIM/Desktop/", "*.txt");
    if(filename == nullptr)
    {
        ui->pushButton->setEnabled(false);
        ui->pushButton_2->setEnabled(false);
        return;
    }
    name.clear();
    QFile *file = new QFile(filename, this);
    file->open(QIODevice::ReadOnly);
    while (!file->atEnd())
    {
        QString line = file->readLine().trimmed();
        if (!line.isEmpty()) { // 避免添加空行
            name.append(line);
        }
    }
    file->close();
    if(thread) delete thread;
    thread = new myQThread(name);
    connect(thread,SIGNAL(setPBtnEnableSignal(bool)),this,SLOT(setPBtnEnable(bool)));
    connect(thread,SIGNAL(updateLabelSignal(QString)),this,SLOT(updateLabel(QString)));
    ui->pushButton->setEnabled(true);
    ui->pushButton_2->setEnabled(true);
}

void MainWindow::on_actionfont_triggered()
{
    bool ok;
    auto font = QFontDialog::getFont(&ok);
    if(ok){
        ui->label->setFont(font);
    }
}

void MainWindow::on_actioncolor_triggered()
{
    QColor mycolor =  QColorDialog::getColor(QColor(255,0,0),this,"设置字体颜色");
    if (mycolor.isValid())
    {
        QString cmd = QString("color: rgb(%1,%2,%3)").arg(mycolor.red()).arg(mycolor.green()).arg(mycolor.blue());
        ui->label->setStyleSheet(cmd);
    }
}
